﻿//获取url中的参数
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	var r = window.location.search.substr(1).match(reg); //匹配目标参数
	if(r != null) return unescape(r[2]);
	return null; //返回参数值
}
var $, layer, form, layedit, upload;
var editindex;
layui.use(['layer', 'upload', 'jquery'], function() {

	$ = layui.jquery;
	layer = layui.layer;
	upload = layui.upload;
	$("#lblid").val(getUrlParam('id'));
	query(getUrlParam('id'));

	//执行实例
	var uploadInst = upload.render({
		elem: '#btnAdd' //绑定元素			
			,
		url: apiUrl + 'api/Admin/UploadFuli' //上传接口			
			,
		multiple: true,
		done: function(src) {
			if(src.code == 0) {
				$("#imgurl").val(src.data.src);
				$("#imgpic").attr("src", src.data.src);
				var com = {
					Fuliid: $("#lblid").val(),
					Imgurl: src.data.src
				}
				AddFuliDetail(com);
			} else {
				layer.msg("上传失败！");
			}
		},
		error: function() {
			//请求异常回调
		}
	});
});

function query(id) {
	if(id == -1) {
		return;
	}
	var index = layer.load(0, {
		shade: false
	});
	$.ajax({
		url: apiUrl + 'api/Admin/GetFuliDetail/' + id,
		type: 'get', //GET
		async: true, //或false,是否异步
		dataType: 'json', //返回的数据格式：json/xml/html/script/jsonp/text
		contentType: 'application/json',
		headers: {
			'Authorization': 'Bearer ' + sessionStorage.getItem("token")
		},
		success: function(jsondata) {
			if(jsondata.Type == 1) {
				$("#ulmain").html("");
				$.each(jsondata.Data, function(index, item) {
					var lihtml = '';
					lihtml += '<li class="liimg layui-col-xs6 layui-col-sm4 layui-col-md3">';
					lihtml += '<div class="layui-card">';
					lihtml += '<div class="layui-card-header">';
					lihtml += '<button class="layui-btn layui-btn-primary layui-btn-sm" onclick="DelFuliDetail(' + item.Id + ')">';
					lihtml += '<i class="layui-icon layui-icon-delete"></i></button>';
					lihtml += '</div>';
					lihtml += '<div class="layui-card-body">';
					lihtml += '<img src="' + item.Imgurl + '">';
					lihtml += '</div>';
					lihtml += '</div></li>';
					$("#ulmain").append(lihtml);
				});
			}
		},
		complete: function() {
			layer.close(index);
		},
		statusCode: {
			401: function() {
				layer.msg("登录信息验证失败，请重新登录");
				location.href = 'login.html';
			}
		}

	});
}
//删除
function DelFuliDetail(id) {
	layer.confirm('确定删除此福利吗？', function(index) { //删除对应行（tr）的DOM结构，并更新缓存
		var index = layer.load(0);
		$.ajax({
			url: apiUrl + 'api/Admin/DelFuliDetail/' + id,
			type: 'post',
			async: true,
			contentType: 'application/json',
			dataType: 'json',
			headers: {
				Authorization: 'Bearer ' + sessionStorage.getItem("token")
			},
			success: function(jsondata) {
				if(jsondata.Type == 1) {
					query($("#lblid").val());
					layer.msg("删除成功");
				} else {
					layer.msg("删除失败");
				}
			},
			complete: function(data, xhr) {
				layer.close(index);
			}

		});
	});

}
//新增
function AddFuliDetail(comm) {
	var index = layer.load(0);
	$.ajax({
		url: apiUrl + 'api/Admin/AddFuliDetail',
		type: 'post', //GET
		async: true, //或false,是否异步
		dataType: 'json',
		contentType: 'application/json',
		headers: {
			Authorization: 'Bearer ' + sessionStorage.getItem("token")
		},
		data: JSON.stringify(comm),
		success: function(jsondata) {
			layer.close(index);
			if(jsondata.Type == 1) {
				query($("#lblid").val());
			}
		}

	});
}